package Week13;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class UDPMenuServer {
//201521123082
	public static void main(String[] args) {
		try (DatagramSocket s = new DatagramSocket(9999);) {
			byte[] buf = new byte[256];
			DatagramPacket packet = new DatagramPacket(buf, buf.length);
			try {
				boolean run = true;
				while (run) {
					System.out.print("\n");
					s.receive(packet);
					packet.setData(new String("1.Show Time\n").getBytes());
					s.send(packet);
					packet.setData(new String("2.Show IP\n").getBytes());
					s.send(packet);
					packet.setData(new String("3.Find File\n").getBytes());
					s.send(packet);
					packet.setData(new String("4.Esc\n").getBytes());
					s.send(packet);
					packet.setData(new String("Please Choose:\n").getBytes());
					s.send(packet);
					buf = new byte[256];
					packet = new DatagramPacket(buf, buf.length);
					s.receive(packet);
					String data = new String(packet.getData()).trim();
					if (data.trim().equals("1")) {
						packet.setData(("Time:"+new Date() + "\n").toString().getBytes());
						s.send(packet);
					}
					if (data.trim().equals("2")) {
						packet.setData(("IP:"+packet.getSocketAddress().toString() + "\n").getBytes());
						s.send(packet);
					}
					if (data.trim().equals("3")) {
						packet.setData((new String("I can't do it now")).getBytes());
					}
					if (data.trim().equals("4")) {
						run = true;
						packet.setData((new String("Success to exit the Menu")).getBytes());
						s.send(packet);
					}
				}
			} finally {
				s.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
